package net.dgg.rz.production.service;

import com.alibaba.fastjson.JSONObject;
import net.dgg.iboss.base.util.JsonUtil;
import net.dgg.iboss.base.util.http.HttpUtility;
import net.dgg.iboss.base.util.http.ResultObj;
import net.dgg.rz.common.constant.SystemCommonConstant;
import net.dgg.rz.common.utils.SearchUtils;
import net.dgg.rz.production.dao.PayProductRecordDao;
import net.dgg.rz.production.entity.PayProductRecord;
import net.dgg.rz.production.utils.OnlyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zyou on 2019/4/1.
 */
@Service
public class PayProductRecordService {

    @Autowired
    private PayProductRecordDao payProductRecordDao;
    @Autowired
    private SearchUtils searchUtils;
    @Value("${salaryUrl}")
    private String salaryUrl;
    /**
     * 根据人员分页查询所有已推送的认款数据
     * @param params
     * @return
     */
    public List<PayProductRecord> royaltyListWithPage(Map params) {
        searchUtils.paramsRoyalty(params);
        List<PayProductRecord> payProductRecords = payProductRecordDao.royaltyListWithPage(params);
        //请求薪酬系统接口查询业绩数据
        if(payProductRecords != null && payProductRecords.size()>0){
            List<String> onlyCodes = new ArrayList<>();
            for(PayProductRecord payProductRecord:payProductRecords){
                String onlyCode = OnlyCodeUtils.getOnlyCode(payProductRecord);
                onlyCodes.add(onlyCode);
            }
            try {
                Map map = new HashMap();
                map.put("onlyCodes",onlyCodes);
                String bas = HttpUtility.postJson(salaryUrl + SystemCommonConstant.FIND_PAYROLLDATA_LIST,map);
                ResultObj resultObj = JsonUtil.json2Obj(bas, ResultObj.class);
                if (resultObj.getCode() == 0) {
                    String resData = resultObj.getData();
                    List<Map> payProductRecordMaps = JsonUtil.json2List(resData,Map.class);
                    for(PayProductRecord payProductRecord:payProductRecords){
                        for(Map payProductRecordMap:payProductRecordMaps){
                            String onlyCode = OnlyCodeUtils.getOnlyCode(payProductRecord);
                            if(onlyCode.equals(payProductRecordMap.get("onlyCode").toString())){
                                payProductRecord.setCalculationItem(JSONObject.parseObject(payProductRecordMap.get("calculationItem").toString()));
                                payProductRecord.setPerformanceData(new BigDecimal(payProductRecordMap.get("performanceData").toString()));
                                continue;
                            }
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return payProductRecords;
    }
}
